//
//  LGPatientNewlyController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/10/23.
//  新增患者

#import "LGPatientNewlyController.h"
//添加咨询记录
#import "LGConsultRecordAddController.h"
#import "LGConsultPatientRepetionView.h"
#import "LGConsultRecordLineView.h"
//信息重复
#import "LGPatientRepetitionView.h"
@interface LGPatientNewlyController ()
/**   */
@property (nonatomic , strong)LGConsultRecordLineView *numberView;
/**   */
@property (nonatomic , strong)LGConsultRecordLineView *nameView;
/**   */
@property (nonatomic , strong)LGConsultRecordLineView *phoneView;
/**   */
@property (nonatomic , strong)LGConsultRecordLineView *cardView;
/**   */
@property (nonatomic , strong)UIButton *sureButton;
/**   */
@property (nonatomic , strong)NSDictionary *dataDic;
@end

@implementation LGPatientNewlyController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationView.title = @"新增患者";
    [self.view addSubview:self.numberView];
    [self.view addSubview:self.nameView];
    [self.view addSubview:self.phoneView];
    [self.view addSubview:self.cardView];
    [self.view addSubview:self.sureButton];
}

-(void)setPatientNum:(NSString *)patientNum{
    _patientNum = patientNum;
    self.numberView.contentTF.text = patientNum;
}

-(void)textFieldEditingChanged:(UITextField *)textField{
    NSString *content = textField.text;
    if (textField == self.nameView.contentTF){
        //姓名
        content = (content.length>10)?[content substringToIndex:10]:content;
        self.nameView.contentTF.text = content;
        
    }else if (textField == self.phoneView.contentTF){
        //手机号
        content = (content.length>11)?[content substringToIndex:11]:content;
        self.phoneView.contentTF.text = content;
        
    }else if (textField == self.cardView.contentTF){
        //身份证号
        content = (content.length>18)?[content substringToIndex:18]:content;
        self.cardView.contentTF.text = content;
    }
}

-(void)sureButtonAction{
    if(self.nameView.contentTF.text.length == 0){
        [TooltipView showMessage:@"请输入患者姓名" offset:0];
        return;
    }
    if(self.phoneView.contentTF.text.length == 0){
        [TooltipView showMessage:@"请输入手机号" offset:0];
        return;
    }
    if(self.cardView.contentTF.text.length > 0 && [ObjectTool checkPhoneNumber:self.phoneView.contentTF.text] == NO){
        [TooltipView showMessage:@"身份证号不正确" offset:0];
    }
    
    [RequestUtil withPOST:@"/doctor/patient.Followup/followupSavePInfo" parameters:@{@"hospital_bano":self.patientNum,@"realname":self.nameView.contentTF.text,@"mobile":self.phoneView.contentTF.text,@"card_no":self.cardView.contentTF.text} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        if(code == 200){
            self.dataDic = returnData;
            NSString *resCode = LGNSString(returnData[@"resCode"]);
            if([resCode isEqualToString:@"0"]){
                //提交成功
                LGConsultRecordAddController *controller = [[LGConsultRecordAddController alloc]init];
                controller.patientid = LGNSString(returnData[@"patient_id"]);
                controller.diseaseCode = LGNSString(returnData[@"disease_code"]);
                controller.levelid = LGNSString(returnData[@"level_id"]);
                controller.isAdd = YES;
                [self.navigationController pushViewController:controller animated:YES];
                
            }else{
                //身份证、手机号重复
                LGPatientRepetitionView *repetView = [[LGPatientRepetitionView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H)];
                repetView.dataDic = returnData;
                [[UIApplication sharedApplication].keyWindow addSubview:repetView];
                __weak typeof(self) weakSelf = self;
                repetView.updatePatientInfo = ^{
                    [weakSelf updatePatientInfoAction];
                };
            }
        }
    } error:^(NSError *error) {
        
    }];
}

-(void)updatePatientInfoAction{
    [RequestUtil withPOST:@"/doctor/patient.Followup/followupUpdatePInfo" parameters:@{@"patient_id":LGNSString(self.dataDic[@"patient_id"]),@"hospital_bano":self.patientNum,@"realname":self.nameView.contentTF.text,@"card_no":self.cardView.contentTF.text,@"mobile":self.phoneView.contentTF.text,@"followup_date":self.followDate} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        if(code == 200){
            NSString *type = LGNSString(returnData[@"jumpType"]);
            if ([type isEqualToString:@"1"]){
                //新增咨询记录
                LGConsultRecordAddController *controller = [[LGConsultRecordAddController alloc]init];
                controller.patientid = LGNSString(returnData[@"patient_id"]);
                controller.diseaseCode = LGNSString(returnData[@"disease_code"]);
                controller.levelid = LGNSString(returnData[@"level_id"]);
                controller.isAdd = YES;
                [self.navigationController pushViewController:controller animated:YES];
                
            }else if ([type isEqualToString:@"2"] || [type isEqualToString:@"3"]){
                LGConsultRecordAddController *controller = [[LGConsultRecordAddController alloc]init];
                controller.patientid = LGNSString(returnData[@"patient_id"]);
                controller.diseaseCode = LGNSString(returnData[@"disease_code"]);
                controller.levelid = LGNSString(returnData[@"level_id"]);
                controller.followDate = self.followDate;
                controller.isAdd = NO;
                [self.navigationController pushViewController:controller animated:YES];
            }
        }
    } error:^(NSError *error) {
        
    }];
}

-(LGConsultRecordLineView *)numberView{
    if(!_numberView){
        _numberView = [[LGConsultRecordLineView alloc] initWithFrame:CGRectMake(viewPix(16), topBarHeight+viewPix(12), Screen_W-viewPix(32), viewPix(54))];
        _numberView.contentTF.userInteractionEnabled = NO;
        _numberView.title = @"登记号";
        _numberView.required = YES;
    }
    return _numberView;
}

-(LGConsultRecordLineView *)nameView{
    if(!_nameView){
        _nameView = [[LGConsultRecordLineView alloc] initWithFrame:CGRectMake(viewPix(16), kMaxY(self.numberView.frame)+viewPix(16), Screen_W-viewPix(32), viewPix(54))];
        _nameView.contentTF.keyboardType = UIKeyboardTypeDefault;
        _nameView.title = @"患者姓名";
        _nameView.required = YES;
        [_nameView.contentTF addTarget:self action:@selector(textFieldEditingChanged:) forControlEvents:UIControlEventEditingChanged];
    }
    return _nameView;
}

-(LGConsultRecordLineView *)phoneView{
    if(!_phoneView){
        _phoneView = [[LGConsultRecordLineView alloc] initWithFrame:CGRectMake(viewPix(16), kMaxY(self.nameView.frame)+viewPix(16), Screen_W-viewPix(32), viewPix(54))];
        _phoneView.contentTF.keyboardType = UIKeyboardTypeNumberPad;
        _phoneView.title = @"手机号";
        _phoneView.required = YES;
        [_phoneView.contentTF addTarget:self action:@selector(textFieldEditingChanged:) forControlEvents:UIControlEventEditingChanged];
    }
    return _phoneView;
}

-(LGConsultRecordLineView *)cardView{
    if(!_cardView){
        _cardView = [[LGConsultRecordLineView alloc] initWithFrame:CGRectMake(viewPix(16), kMaxY(self.phoneView.frame)+viewPix(16), Screen_W-viewPix(32), viewPix(54))];
        _cardView.contentTF.keyboardType = UIKeyboardTypeDefault;
        _cardView.title = @"身份证号";
        _cardView.required = NO;
        [_cardView.contentTF addTarget:self action:@selector(textFieldEditingChanged:) forControlEvents:UIControlEventEditingChanged];
    }
    return _cardView;
}

-(UIButton *)sureButton{
    if(!_sureButton){
        _sureButton = [UIButton buttonWithTitle:@"确定" topMargin:Screen_H-viewPix(80)-bottomSafeBarHeight/4.0];
        [_sureButton addTarget:self action:@selector(sureButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _sureButton;
}


@end
